package com.xmut.bs.handaccount.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.activity.CommentActivity;
import com.xmut.bs.handaccount.bean.GoodsDetail;
import com.xmut.bs.handaccount.bmob.Goods;
import com.xmut.bs.handaccount.util.GlideCircleTransform;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

import static com.xmut.bs.handaccount.base.Constants.PARAM_GOODS;

/**
 * Description
 * Created by Dollkey on 2018/4/17.
 */

public class GoodsDetailAdapter extends MultiItemTypeAdapter<GoodsDetail> {

    public GoodsDetailAdapter(Context context, List<GoodsDetail> datas, final Goods goods) {
        super(context, datas);
        addItemViewDelegate(new ItemViewDelegate<GoodsDetail>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_article_img;
            }

            @Override
            public boolean isForViewType(GoodsDetail item, int position) {
                return item.getItemType() == 2;
            }

            @Override
            public void convert(ViewHolder holder, GoodsDetail t, int position) {
                ImageView view = holder.getView(R.id.img);
                GlideApp.with(view).load(t.getDetailUrl()).fitCenter().into(view);
            }
        });
        addItemViewDelegate(new ItemViewDelegate<GoodsDetail>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_comment;
            }

            @Override
            public boolean isForViewType(GoodsDetail item, int position) {
                return item.getItemType() == 1;
            }

            @Override
            public void convert(ViewHolder holder, GoodsDetail t, int position) {
                holder.setText(R.id.time, t.getComment().getCreatedAt());
                holder.setText(R.id.content, t.getComment().getComment());
                holder.setText(R.id.username, t.getComment().getUser().getNickName());
                ImageView avatarImg = holder.getView(R.id.avatar_img);

                GlideApp.with(avatarImg).load(t.getComment().getUser().getAvatar())
                        .placeholder(R.mipmap.ic_head_img).error(R.mipmap.ic_head_img)
                        .transform(new GlideCircleTransform(mContext)).override(60, 60).into(avatarImg);
            }
        });
        addItemViewDelegate(new ItemViewDelegate<GoodsDetail>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_all_comment;
            }

            @Override
            public boolean isForViewType(GoodsDetail item, int position) {
                return item.getItemType() == 0;
            }

            @Override
            public void convert(ViewHolder holder, GoodsDetail t, int position) {
                holder.setText(R.id.text, "用户评论");
                TextView view = holder.getView(R.id.text_right);
                view.setVisibility(View.VISIBLE);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mContext.startActivity(new Intent(mContext, CommentActivity.class).putExtra(PARAM_GOODS, goods));
                    }
                });
            }
        });
        addItemViewDelegate(new ItemViewDelegate<GoodsDetail>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_all_comment;
            }

            @Override
            public boolean isForViewType(GoodsDetail item, int position) {
                return item.getItemType() == 3;
            }

            @Override
            public void convert(ViewHolder holder, GoodsDetail t, int position) {
                holder.setText(R.id.text, "商品详情");
            }
        });
    }
}
